/******* 公共JS ********/

//小站选项相关
//=============================================================
//添加选项
function aj_add_options(siteid, siteurl) {
	//清除添加提示
	aj_save_sitetips(siteid);
	
	var oplen = $("#options > li").length;
	if(oplen >= 5) {
		//alert("最多只可添加5个选项");
		$("#banner_msg").html("最多只可添加5个选项");
		return false;
	}
	var opnum = oplen+1;
	var bignum = Array("一", "二", "三", "四", "五");
	var opname = '选项'+bignum[opnum-1];
	
	//ajax添加选项
	var opid = "";
	$.ajax({
		type:"POST",
		url:"/ajax/add_options/",
		data:"siteid="+siteid+"&opname="+opname,
		success:function(data){
			opid = parseInt(data);
			if(data) {
				var option = '<li><a href="'+siteurl+opid+'">'+opname+'</a></li>';
				$("#options").append(option);	
			}
		}
	});
}

//编辑选项名称
function aj_edit_option(optid, opname) {
	if(opname == "") {
		alert('请输入选项名称');
		return false;
	} 
	//验证字符
	var pattern = /^[\da-zA-Z\u0391-\uFFE5]{1,10}$/;
	if(!pattern.test(opname)) {
		alert("输入字符长度超出限制或非法字符导致保存失败");
		return false;
	}
	if(opname.length == 1) {
		//opname += " ... ";
		var pattern = /\w/;
		if(pattern.test(opname)) {
			opname += "...";
		}
	}
	$.ajax({
		type:"POST",
		url:"/ajax/edit_option/",
		data:"optid="+optid+"&opname="+opname,
		success:function(data){
		}
	});
}

//删除选项名称
function aj_del_option(optid, siteurl) {
	$.ajax({
		type:"POST",
		url:"/ajax/del_option/",
		data:"optid="+optid,
		success:function(data){
			window.location.href = siteurl;
		}
	});
}

//添加选项应用
function aj_add_plugins(modid, modname, opid, url, domain ) {
	var oplen = $("#pluginsarea > li").length;
	if(oplen >= 8) {
		$("#plugins_msg").html("1个选项最多只可添加8个应用");
		return false;
	} else {
		$("#plugins_msg").html("");
	}
	var opnum = oplen+1;
	//var option = '<li id="op'+opnum+'"><a href="#">选项'+opnum+'</a></li>';
	
	//ajax添加选项应用
	var plid = "";
	$.ajax({
		type:"POST",
		url:"/ajax/add_plugins/",
		data:"opid="+opid+"&plname="+modname+"&moduleid="+modid,
		success:function(data){
			if(data) {
				plid = parseInt(data);
				var option = '<li id="plugin_'+plid+'">';
				option += '<h3><span class="f22px" id="pltitle_'+plid+'">'+modname;
				if(modid != 1 && modid != 3) {
					option += ' [公共]';
				}
				option += '</span>';
				if(modid != 1 && modid != 3) {
					option += ' <a class="size_14_font" href="'+url+domain+'/plugin/list/'+plid+'">(全部)</a>';
				}
				option += '  <a class="size_14_font" href="'+url+domain+'/plugin/add/'+plid+'">(新建)</a>';
				option += ' <span class="setUp"><a href="javascript:void(0)" onclick="aj_showedit_plugin('+plid+')">(设置)</a></span>';
				option += '</h3></li>';
				option += '<div id="plwindow_'+plid+'" class="applyBox">';
				option += '<label>命名此应用:</label><input type="text" maxlength="10" class="inputTxt" name="plname_'+plid+'" id="plname_'+plid+'" value="'+modname+'" />';
				option += '<input type="button" class="smallKey" value="保存" onclick="aj_edit_plugin('+plid+');" />';
				option += ' <input type="button" class="smallKey" value="取消" onclick="aj_hidedit_plugin('+plid+');" />';
				var deljsstr = "return confirm('是否要删除？');";
				option += '<span><a href="javascript:aj_del_plugin('+plid+');" onclick="'+deljsstr+'" class="deleteBox">删除此应用</a></span><div class="hackbox"></div>';
				if(modid != "3" && modid != "1") {
					option += '<label><input type="checkbox" name="public_'+plid+'" id="public_'+plid+'" value="1" checked>公共创建(让粉丝参与本应用的内容创建)</label>';
				}
				option += '</div><div class="blank18"></div>';
				$("#pluginsarea").prepend(option);
			
				//提示信息
				 $("#plugins_msg_"+modid).html("添加成功");
				 $("#plugins_msg_"+modid).fadeIn(function() {
					setTimeout(function() {
						 $("#plugins_msg_"+modid).fadeOut();
					}, 1000);
				});
			}
		}
	});
}

//编辑选项应用名称
function aj_edit_plugin(plid) {
	var plname = $("#plname_"+plid).val();
	var ispublic = $("input[name='public_"+plid+"']:checked").val();
	if(!ispublic) ispublic = 0;

	//验证字符
	var pattern = /^[ \da-zA-Z\u0391-\uFFE5]{1,10}$/;
	if(!pattern.test(plname)) {
		alert("请输入字母、中文或数字");
		return;
	}
	$.ajax({
		type:"POST",
		url:"/ajax/edit_plugin/",
		data:"plid="+plid+"&plname="+plname+"&ispublic="+ispublic,
		success:function(data){
			if(data) {
				if(ispublic == '1') {
					plname += "[公共]";
				}
				$("#pltitle_"+plid).html(plname);
				aj_hidedit_plugin(plid);
			}
		}
	});
}

//删除选项应用
function aj_del_plugin(plid) {
	$.ajax({
		type:"POST",
		url:"/ajax/del_plugin/",
		data:"plid="+plid,
		success:function(data){
			$("#plwindow_"+plid).remove();
			$("#plugin_"+plid).remove();
			$("#blank_"+plid).remove();
		}
	});
}

//显示应用编辑选项
function aj_showedit_plugin(plid) {
	$("#plwindow_"+plid).toggle();
}

//隐藏应用编辑选项
function aj_hidedit_plugin(plid) {
	$("#plwindow_"+plid).hide();
}


//显示编辑选项
function aj_showedit_options() {
	$("#optionswindow").slideDown();
	if($("#optiontips") != null) {
		$("#optiontips").slideUp();
	}
}

//隐藏编辑选项
function aj_hidedit_options() {
	$("#optionswindow").slideUp();

	if($("#optiontips") != null && $("#pluginsarea li").eq(0).length == 0) {
		$("#optiontips").slideDown();
	}
} 

//小站设置相关
//=============================================================
//修改描述信息
function aj_save_sitedesc() {
	
	//检测分类
	if($("#subclass").val() == "") {
		$("#class_msg").html("请选择分类");
		$("#save_msg").html("");
		return false;
	} else {
		$("#class_msg").html("");
	}
	
	//获取标签选项
	var f_str = '';
	var tagnum = 0;
	$(":checkbox[@name='tag']").each(function(){
	   if($(this).attr("checked")==true){
	    f_str += $(this).attr("value")+',';
	    tagnum++;
	   }
	})
	if(tagnum>5) {
		$("#tag_msg").html("最多选5个");
		return false;
	}
	//描述长度
	if(fucCheckLength($("#description").val()) > 40){
		alert("描述长度超过限制");
		return false;
	}
	$.ajax({
		type:"POST",
		url:"/ajax/save_sitedesc/",
		data:"siteid="+$("#siteid").val()+"&desc="+$("#description").val()+"&classid="+$("#subclass").val()+"&tag="+f_str,
		success:function(data){
			var obj = $("#save_msg");
			if(data != "") {
				obj.html(data);	
				$("#tag_msg").html("&nbsp;");
				$("#save_msg").fadeIn(function() {  
		   			 setTimeout(function() {  
		   			 $("#save_msg").fadeOut();  
		   			}, 1000);  
	   			}); 
			} 
		}
	});
}

//修改LOGO
function aj_save_sitelogo(siteid) {
	$.ajax({
		type:"POST",
		url:"/ajax/save_sitelogo/",
		data:"siteid="+siteid+"&x1="+$("#x1").val()+"&x2="+$("#x2").val()+"&y1="+$("#y1").val()+"&y2="+$("#y2").val()+"&w="+$("#w").val()+"&h="+$("#h").val(),
		success:function(data){
			var obj = $("#save_msg2");
			if(data != "") {
				obj.html(data);	
				$("#save_msg2").fadeIn(function() {  
		   			 setTimeout(function() {  
		   			 $("#save_msg2").fadeOut();  
		   			}, 1000);  
	   			}); 
			} 
		}
	});
}

//修改添加选项提示
function aj_save_sitetips(siteid) {
	$.ajax({
		type:"POST",
		url:"/ajax/save_sitetips/",
		data:"siteid="+siteid,
		success:function(data){
			$("#addoptips").hide();
		}
	});
}

//预览风格
function aj_preview_siteskin(k, background, bannerbg, color) {
	$("body").css("background", "url("+background+") repeat-x left top");
	$("#nav").css("background", "url("+bannerbg+") repeat-x left top");
	$("#nav > ul > li > a").css("color", color);
	$("#nav > ul > li > a > b").css("color", color);
	$("#listskins > li > a > img").removeClass("skinborder");
	$("#skin_"+k+" > img").addClass("skinborder");
}

//修改风格
function aj_save_siteskin(siteid) {
	var skinid = $(".skinborder").attr("id");
	$.ajax({
		type:"POST",
		url:"/ajax/save_siteskin/",
		data:"siteid="+siteid+"&skinid="+skinid,
		success:function(data){
			var obj = $("#skin_msg");
			if(data != "") {
				obj.html(data);	
				$("#skin_msg").fadeIn(function() {  
		   			 setTimeout(function() {  
		   			 $("#skin_msg").fadeOut();  
		   			}, 1000);  
	   			}); 
			} 
		}
	});
}

//修改连接颜色
function aj_change_linkcolor(siteid, color) {
	$.ajax({
		type:"POST",
		url:"/ajax/change_linkcolor/",
		data:"siteid="+siteid+"&color="+color,
		success:function(data){
			var obj = $("#color_msg");
			if(data != "") {
				obj.html(data);	
				obj.show();
			} 
		}
	});
}


//添加小站关注
function aj_set_fans(siteid, userid, siteuserid) {
	if(!userid) {
		alert("请选登陆");
		return false;
	}
	$.ajax({
		type:"POST",
		url:"/ajax/set_fans/",
		dataType:"json",
		data:"siteid="+siteid+"&userid="+userid+"&siteuserid="+siteuserid,
		success:function(data){
				$("#fans_msg").html(data.button);
				$("#fanscount").html(data.result);
		}
	});
}

//取消小站关注
function aj_unset_fans(siteid, userid) {
	$.ajax({
		type:"POST",
		url:"/ajax/unset_fans/",
		dataType:"json",
		data:"siteid="+siteid+"&userid="+userid,
		success:function(data){
			$("#fans_msg").html(data.button);
			$("#fanscount").html(data.result);
		}
	});
}

//提示不可取消对自己的关注
function aj_unset_msg()
{
	$("#set_msg").html("不能取消");
	$("#set_msg").fadeIn(function() {
		setTimeout(function() {
			$("#set_msg").fadeOut();
		}, 1000);
	});
}

//删除关注
function aj_delete_fans(siteid, userid, k) {
	$.ajax({
		type:"POST",
		url:"/ajax/unset_fans/",
		data:"siteid="+siteid+"&userid="+userid,
		success:function(data){
			if(data != "") {
				$("li").remove("#fans_"+k);
			} 
		}
	});
}

//删除小站
function aj_delete_site(siteid) {
	$.ajax({
		type:"POST",
		url:"/ajax/delete_site/",
		data:"siteid="+siteid,
		success:function(data){
			if(data != "") {
				$("#mysite").hide();
			} 
		}
	});
}

//赞美用户
function aj_set_digg(domain, userid, author, dataid) {
	if(!userid) {
		alert("请选登陆");
		return false;
	}
	var option = $("input[name='option']:checked").val();
	if(!option) {
		alert("请先选择");
		return false;
	}
//	$(document).progressDialog.showDialog();
	$.ajax({
		type:"POST",
		url:"/ajax/set_digg/",
		data:"userid="+userid+"&author="+author+"&dataid="+dataid+"&option="+option,
		success:function(data){
			//$(":radio").hide();
			//$("#diggcount").html(parseInt($("#diggcount").html())+1);
			window.location= "/site/"+domain+"/forum/show/"+dataid;
		}
	});
}

//=============================================================
